package com.hdit.guimei.web;

import com.github.pagehelper.PageInfo;
import com.hdit.guimei.domain.Goods;
import com.hdit.guimei.domain.Goods_detail;
import com.hdit.guimei.service.GoodsService;
import com.hdit.guimei.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.hdit.guimei.util.PageUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 冰镇小可乐
 * @ClassNameGoodsController
 * @Description TODO
 * @Date 2021/1/19 14:22
 * Version 1.0
 */
@Controller
public class GoodsController {
    @Autowired
    private GoodsService service;
    /***
     * @Method 根据商品名称或者商品品牌进行模糊查询
     * @Author 冰镇小可乐
     * @params [parm, pageNum, pageSize]
     * @Return java.util.List<com.hdit.guimei.domain.Goods>
     * @Exception
     * @Date 2021/1/19 14:38
     */
    @RequestMapping("/goods/findGoodsBy")
    @ResponseBody
    Map findGoodsBy(@RequestBody Map<String,String> params) {
        Map map=new HashMap();
        System.out.println("请求了");
        String param=params.get("param");
        param = "%" + param + "%";
        System.out.println(param);
        List<Goods> goodsList = service.findGoodsBy(param,Integer.parseInt(params.get("pageNum")),
                Integer.parseInt(params.get("pageSize")));
        map.put("goodslist",goodsList);
        map.put("total",goodsList.size());
        return map;
    }

    /***
     * @Method 根据商品价格区间进行商品信息查询
     * @Author 冰镇小可乐
     * @params [start, last, pageNum, pageSize]
     * @Return java.util.List<com.hdit.guimei.domain.Goods>
     * @Exception
     * @Date 2021/1/19 14:52
     */
    @RequestMapping("/goods/findGoodsByPrice")
    @ResponseBody
    List<Goods> findGoodsByPrice(Double start, double last, int pageNum, int pageSize) {
        System.out.println("请求了价格区间");
        List<Goods> list = service.findGoodsByPrice(start, last, pageNum, pageSize);
        System.out.println(list);
        return list;
    }
    /**
    * @Method: addGoods
    * @函数(方法)功能: 添加商品信息（多图片）
    * @Author: 李嘉明
    * @params:  [goods, picture1, picture2, picture3, picture4, picture5]
    * @Return: int
    * @Exception:
    * @Date: 2021/1/20 21:36
    */
    @RequestMapping("/goods/addGoods")
    @ResponseBody
    int addGoods(Goods goods, MultipartFile picture1, MultipartFile picture2, MultipartFile picture3, MultipartFile picture4, MultipartFile picture5) {
        if (picture1!=null&&!picture1.isEmpty()) {
            String picture1_url = new OssUtil().uploadDocument(picture1, new SubStringUtil().SubFileName(picture1.getOriginalFilename(), "."));
            goods.setPicture_one(picture1_url);
            System.out.println(picture1_url);
        }

        if (picture2!=null&&!picture2.isEmpty()) {
            String picture2_url = new OssUtil().uploadDocument(picture2, new SubStringUtil().SubFileName(picture2.getOriginalFilename(), "."));
            goods.setPicture_two(picture2_url);
            System.out.println(picture2_url);
        }
        if (picture3!=null&&!picture3.isEmpty()) {
            String picture3_url = new OssUtil().uploadDocument(picture3, new SubStringUtil().SubFileName(picture3.getOriginalFilename(), "."));
            goods.setPicture_three(picture3_url);
            System.out.println(picture3_url);
        }
        if (picture4!=null&&!picture4.isEmpty()) {
            String picture4_url = new OssUtil().uploadDocument(picture4, new SubStringUtil().SubFileName(picture4.getOriginalFilename(), "."));
            goods.setPicture_four(picture4_url);
            System.out.println(picture4_url);
        }
        if (picture5!=null&&!picture5.isEmpty()) {
            String picture5_url = new OssUtil().uploadDocument(picture5, new SubStringUtil().SubFileName(picture5.getOriginalFilename(), "."));
            goods.setPicture_five(picture5_url);
            System.out.println(picture5_url);
        }
            System.out.println(goods);
            return service.addGoods(goods);
}
    /***
    * @Method 查询商品销量
    * @Author 冰镇小可乐
    * @params  [param]
    * @Return java.util.List<com.hdit.guimei.domain.Goods>
    * @Exception
    * @Date 2021/1/20 15:19
    */
    @RequestMapping("/goods/findSalesVolume")
    @ResponseBody
    List<Goods> findSalesVolume(String param){
        List<Goods> list =service.findGoodIdBy(param);
        return list;
     }
     @RequestMapping("/goods/findgoodbyclass")
     @ResponseBody
    List<Goods> findgoodbyclass(String categoryName){
         System.out.println(categoryName);
         List<Goods> goodsList = service.findgoodsbyclass(categoryName);
         System.out.println(goodsList);
         return goodsList;
     }
    @RequestMapping("/goods/findAllGoods")
    @ResponseBody
    ResponseData<PageInfo>  findAllGoods( int pageNum,int pageSize){
        System.out.println(pageNum);
        System.out.println(pageSize);
        PageInfo<Goods> pageInfo = service.findAllGoods(pageNum,pageSize);
        ResponseData<PageInfo> res = new ResponseData<>();
        res.setCode(200);
        res.setMess("ok");
        res.setData(pageInfo);
        return res;
    }
    @RequestMapping("/goods/findAllGoodss")
    @ResponseBody
    Map findAllGoods(@RequestBody Map<String,String> params){
        Map map=new HashMap();
        List<Goods> goodsList1=service.findAllGoods();
        if (!goodsList1.isEmpty()){
            List<Goods> goodsList= PageListUtil.Util(goodsList1,Integer.parseInt(params.get("pageNum")),
                    Integer.parseInt(params.get("pageSize")));
            map.put("goodsList",goodsList);
            map.put("total",goodsList1.size());
        }
        System.out.println(map);
        return map;
    }
    /**
     * @Method: updateGoods
     * @函数(方法)功能: 更新商品信息（数据回填）
     * @Author: 李嘉明
     * @params:  [goods, picture1, picture2, picture3, picture4, picture5]
     * @Return: int
     * @Exception:
     * @Date: 2021/1/21 19:54
     */
    @RequestMapping("/updateGoods")
    @ResponseBody
    public int updateGoods(Goods goods, MultipartFile picture1, MultipartFile picture2, MultipartFile picture3, MultipartFile picture4, MultipartFile picture5){
        System.out.println(goods);
        if (picture1!=null&&!picture1.isEmpty()) {
            String picture1_url = new OssUtil().uploadDocument(picture1, new SubStringUtil().SubFileName(picture1.getOriginalFilename(), "."));
            goods.setPicture_one(picture1_url);
            System.out.println(picture1_url);
        }

        if (picture2!=null&&!picture2.isEmpty()) {
            String picture2_url = new OssUtil().uploadDocument(picture2, new SubStringUtil().SubFileName(picture2.getOriginalFilename(), "."));
            goods.setPicture_two(picture2_url);
            System.out.println(picture2_url);
        }
        if (picture3!=null&&!picture3.isEmpty()) {
            String picture3_url = new OssUtil().uploadDocument(picture3, new SubStringUtil().SubFileName(picture3.getOriginalFilename(), "."));
            goods.setPicture_three(picture3_url);
            System.out.println(picture3_url);
        }
        if (picture4!=null&&!picture4.isEmpty()) {
            String picture4_url = new OssUtil().uploadDocument(picture4, new SubStringUtil().SubFileName(picture4.getOriginalFilename(), "."));
            goods.setPicture_four(picture4_url);
            System.out.println(picture4_url);
        }
        if (picture5!=null&&!picture5.isEmpty()) {
            String picture5_url = new OssUtil().uploadDocument(picture5, new SubStringUtil().SubFileName(picture5.getOriginalFilename(), "."));
            goods.setPicture_five(picture5_url);
            System.out.println(picture5_url);
        }
        System.out.println(goods);
        return service.updateGoods(goods);
    }
    /**
     * @Method: findGoodsByGoodsId
     * @函数(方法)功能: 根据商品id进行查询商品信息
     * @Author: 李嘉明
     * @params:  [good_id]
     * @Return: com.hdit.guimei.domain.Goods
     * @Exception:
     * @Date: 2021/1/21 19:58
     */
    @RequestMapping("/findGoodsByGoodsId")
    @ResponseBody
    public Goods findGoodsByGoodsId(@RequestParam("good_id") int good_id){
        System.out.println(good_id);
        return service.findGoodsByGoodsId(good_id);
    }
    @RequestMapping("findGoodsByCategory")
    @ResponseBody
    public Map findGoodsByCategory(@RequestBody Map<String,String> params) {
        Map map=new HashMap();
        List<Goods> goodsList1=service.findGoodsByCategory(Integer.parseInt(params.get("category_id")));
        if (!goodsList1.isEmpty()){
            List<Goods> goodsList= PageListUtil.Util(goodsList1,Integer.parseInt(params.get("pageNum")),
                    Integer.parseInt(params.get("pageSize")));
            map.put("goodsList",goodsList);
            map.put("total",goodsList1.size());
        }
        return map;
    }

    @RequestMapping("findgoodpicture")
    @ResponseBody
    List<Goods> findgoodpicture(@RequestParam int good_id){
        System.out.println(service.findgoodpicture(good_id));
        return service.findgoodpicture(good_id);
    }
}

